'use strict';

const rdsutility = require('../common/rdsutility');
const assert = require('assert');
const dbutility = require('../common/dbutility');
const after = require('after');
const expModel = dbutility.expModel;
const cabModel = dbutility.cabModel;
const redis = require('redis');
const redisClient = redis.createClient();

describe('rdsutility', function() {
    before(function(done) {
        done = after(2, done);

        redisClient.del('tel4Hset:1234', (err, res) => {
            assert.ifError(err);
            redisClient.hmset('tel4Hset:1234', '11111111234', '张三', '22222221234', '王五', '33333331234', '李四', (err, res) => {
                assert.ifError(err);
                done();
            });
        });

        redisClient.del('cabPwdSet', (err, res) => {
            assert.ifError(err);
            redisClient.sadd('cabPwdSet', '1234', 'adsa', '12al', (err, res) => {
                assert.ifError(err);
                done();
            });
        });
    });

    describe('getReTelNames(tel4)', function() {
        it('可以根据电话号码后四位检索到所有相关收件人的信息', function(done) {
            rdsutility.getReTelNames('1234').then(function(reTelNames) {
                let expected = [{ expReTel: '11111111234', expReName: '张三' }, { expReTel: '22222221234', expReName: '王五' }, { expReTel: '33333331234', expReName: '李四' }].sort();
                let actual = reTelNames.sort();
                let actualStr = JSON.stringify(actual);
                let expectedStr = JSON.stringify(expected);

                assert.equal(expectedStr, actualStr);
                done();
            }, function(err) {
                assert.ifError(err);
            });
        });
    });

    describe('addReTelName(reTel, reName)', function() {
        it('可以正确的将新的收件人信息加入电话号码哈希表中', function(done) {
            rdsutility.addReTelName('44444445678', '刘六').then(function() {
                redisClient.hget('tel4Hset:5678', '44444445678', (err, res) => {
                    assert.equal('刘六', res);
                    done();
                });
            }, function(err) {
                assert.ifError(err);
            });
        });
    });

    describe('hasCabPwdInUse(cabPwd)', function() {
        it('当输入未使用的取件码时，可以正确的判断取件码未使用', function(done) {
            rdsutility.hasCabPwdInUse('1235').then(function(inuse) {
                assert.equal(false, inuse);
                done();
            }, function(err) {
                assert.ifError(err);
            });
        });

        it('当输入已使用的取件码时，可以正确的判断取件码已使用', function(done) {
            rdsutility.hasCabPwdInUse('1234').then(function(inuse) {
                assert.equal(true, inuse);
                done();
            }, function(err) {
                assert.ifError(err);
            });
        });
    });

    describe('addCabPwdInUse(cabPwd)', function() {
        it('可以正确的将新的快递取件码加入到快递柜取件码集合中', function(done) {
            rdsutility.addCabPwdInUse('1236').then(function() {
                redisClient.sismember('cabPwdSet', '1236', (err, res) => {
                    assert.ifError(err);
                    assert.equal(true, res);
                    done();
                });
            }, function(err) {
                assert.ifError(err);
            });
        });
    });

    describe('initReTelNamesHSet()', function() {
        before(function(done) {
            done = after(6, done);

            redisClient.del('tel4Hset:????', (err, res) => {
                assert.ifError(err);
                done();
            });

            expModel.remove({}, (err, doc) => {
                assert.ifError(err);
                done();
            });

            expModel.create({ expReTel: '10000000000', expReName: 'expReName1' }, (err, doc) => {
                assert.ifError(err);
                done();
            });

            expModel.create({ expReTel: '20000000000', expReName: 'expReName2' }, (err, doc) => {
                assert.ifError(err);
                done();
            });

            expModel.create({ expReTel: '50000000001', expReName: 'expReName4' }, (err, doc) => {
                assert.ifError(err);
                done();
            });

            expModel.create({ expReTel: '60000000001', expReName: 'expReName5' }, (err, doc) => {
                assert.ifError(err);
                done();
            });
        });

        it('可以正确的初始化Redis中电话号码哈希表', function(done) {
            done = after(2, done);

            rdsutility.initReTelNamesHSet(function() {
                redisClient.hgetall('tel4Hset:0000', (err, res) => {
                    assert.ifError(err);
                    let expected = { '10000000000': 'expReName1', '20000000000': 'expReName2' }
                    assert.deepEqual(expected, res);
                    done();
                });

                redisClient.hgetall('tel4Hset:0001', (err, res) => {
                    assert.ifError(err);
                    let expected = { '50000000001': 'expReName4', '60000000001': 'expReName5' }
                    assert.deepEqual(expected, res);
                    done();
                });
            });
        });

        after(function(done) {
            done = after(2, done);
            
            redisClient.del('tel4Hset:????', (err, res) => {
                assert.ifError(err);
                done();
            });

            expModel.remove({}, (err, doc) => {
                assert.ifError(err);
                done();
            });
        });
    });

    describe('initCabPwdInUseSet()', function() {
        before(function(done) {
            done = after(5, done);

            redisClient.del('cabPwdSet', (err, res) => {
                assert.ifError(err);
                done();
            });

            cabModel.remove({}, (err, doc) => {
                assert.ifError(err);
                done();
            });

            cabModel.create({ cabPwd: '1234', cabInUse: true }, (err, doc) => {
                assert.ifError(err);
                done();
            });

            cabModel.create({ cabPwd: 'asdf', cabInUse: true }, (err, doc) => {
                assert.ifError(err);
                done();
            });

            cabModel.create({ cabPwd: '1qaz', cabInUse: false }, (err, doc) => {
                assert.ifError(err);
                done();
            });
        });

        it('可以正确的初始化Redis中快递柜取件码集合', function(done) {
            rdsutility.initCabPwdInUseSet(function() {
                redisClient.smembers('cabPwdSet', (err, res) => {
                    assert.ifError(err);
                    let expected = ['1234', 'asdf'];
                    assert.equal(JSON.stringify(expected.sort()), JSON.stringify(res.sort()));
                    done();
                });
            });
        });
        
        after(function(done){
            done = after(2, done);

            redisClient.del('cabPwdSet', (err, res) => {
                assert.ifError(err);
                done();
            });

            cabModel.remove({}, (err, doc) => {
                assert.ifError(err);
                done();
            });
        });
    });
});